<?php

global $_GPC;
global $_W;
$shopid = check_right(0);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
load()->model('mc');
if ($operation == 'display') {
    $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by displayorder asc,id desc');
    $categoryary = array();
    foreach ($category as $row) {
        $categoryary[$row['id']] = $row['title'];
    }
    if ($category[0]['id']) {
        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate =\'' . $category[0]['id'] . '\' order by status desc,displayorder asc,id desc');
    }
} else {
    if ($operation == 'getpcate') {
        $id = intval($_GPC['id']);
        if (!$id) {
            exit(json_encode(array('success' => false, 'msg' => '请选择要修改的内容')));
        }
        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
        exit(json_encode(array('success' => true, 'item' => $item)));
    } else {
        if ($operation == 'getpcatetable') {
            $pcate = intval($_GPC['pcate']);
            if (!$pcate) {
                exit(json_encode(array('success' => false, 'msg' => '请选择区域')));
            }
            $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=:a order by status desc,displayorder asc,id asc', array(':a' => $pcate));
            exit(json_encode(array('success' => true, 'list' => $list)));
        } else {
            if ($operation == 'addpcate') {
                $title = trim($_GPC['pkeyword']);
                if (!$title) {
                    exit(json_encode(array('success' => false, 'msg' => '请输入区域名称')));
                }
                $num = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a and title=:b ', array(':a' => $shopid, ':b' => $title));
                if ($num) {
                    exit(json_encode(array('success' => false, 'msg' => '区域名称重复，请重新输入')));
                }
                $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $title);
                pdo_insert('j_hmoney_tablecategory', $data);
                exit(json_encode(array('success' => true)));
            } else {
                if ($operation == 'editpcate') {
                    $id = intval($_GPC['id']);
                    $title = trim($_GPC['title']);
                    $printerip = trim($_GPC['printerip']);
                    if (!$id) {
                        exit(json_encode(array('success' => false, 'msg' => '请选择要修改的内容')));
                    }
                    if (!$title) {
                        exit(json_encode(array('success' => false, 'msg' => '请输入区域名称')));
                    }
                    $num = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a and title=:b and id<>:c', array(':a' => $shopid, ':b' => $title, ':c' => $id));
                    if ($num) {
                        exit(json_encode(array('success' => false, 'msg' => '区域名称重复，请重新输入')));
                    }
                    $data = array('title' => $title, 'printerip' => $printerip);
                    pdo_update('j_hmoney_tablecategory', $data, array('id' => $id));
                    exit(json_encode(array('success' => true)));
                } else {
                    if ($operation == 'resetpcateorder') {
                        $idary = explode(',', $_GPC['displayorder']);
                        $sql = 'update ' . tablename('j_hmoney_tablecategory') . ' SET `displayorder` = CASE id ';
                        $i = 0;
                        while ($i < count($idary)) {
                            $sql .= ' WHEN ' . $idary[$i] . ' THEN ' . ($i + 1) . ' ';
                            $i = $i + 1;
                        }
                        $sql .= ' END ';
                        $sql .= ' WHERE id IN (' . $_GPC['displayorder'] . ') ';
                        pdo_run($sql);
                        exit(json_encode(array('success' => true)));
                    } else {
                        if ($operation == 'resettableorder') {
                            $idary = explode(',', $_GPC['displayorder']);
                            $sql = 'update ' . tablename('j_hmoney_table') . ' SET `displayorder` = CASE id ';
                            $i = 0;
                            while ($i < count($idary)) {
                                $sql .= ' WHEN ' . $idary[$i] . ' THEN ' . ($i + 1) . ' ';
                                $i = $i + 1;
                            }
                            $sql .= ' END ';
                            $sql .= ' WHERE id IN (' . $_GPC['displayorder'] . ') ';
                            pdo_run($sql);
                            exit(json_encode(array('success' => true)));
                        } else {
                            if ($operation == 'gettable') {
                                $id = intval($_GPC['id']);
                                if (!$id) {
                                    exit(json_encode(array('success' => false, 'msg' => '请选择要修改的内容')));
                                }
                                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                                exit(json_encode(array('success' => true, 'item' => $item)));
                            } else {
                                if ($operation == 'posttable') {
                                    $id = intval($_GPC['id']);
                                    if (empty($_GPC['title'])) {
                                        exit(json_encode(array('success' => false, 'msg' => '名称不能为空')));
                                    }
                                    if (empty($_GPC['pcate'])) {
                                        exit(json_encode(array('success' => false, 'msg' => '请选择区域')));
                                    }
                                    $data = array('weid' => $_W['uniacid'], 'title' => $_GPC['title'], 'groupid' => intval($shopid), 'displayorder' => intval($_GPC['displayorder']), 'pcate' => intval($_GPC['pcate']), 'perperson' => intval($_GPC['perperson']), 'description' => htmlspecialchars_decode($_GPC['description']), 'maxnum' => intval($_GPC['maxnum']), 'status' => intval($_GPC['status']));
                                    if (!empty($id)) {
                                        $isused = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' and title=:a  and id<>:b', array(':a' => $_GPC['title'], ':b' => $id));
                                        if ($isused) {
                                            exit(json_encode(array('success' => false, 'msg' => '名称重复，请修改')));
                                        }
                                        pdo_update('j_hmoney_table', $data, array('id' => $id));
                                    } else {
                                        $isused = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' and title=:a ', array(':a' => $_GPC['title']));
                                        if ($isused) {
                                            exit(json_encode(array('success' => false, 'msg' => '名称重复，请修改')));
                                        }
                                        pdo_insert('j_hmoney_table', $data);
                                        $id = pdo_insertid();
                                    }
                                    $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                                    exit(json_encode(array('success' => true, 'item' => $item)));
                                } else {
                                    if ($operation == 'deletepcate') {
                                        $id = intval($_GPC['id']);
                                        if ($id) {
                                            $isUsed = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=:a', array(':a' => $id));
                                            if ($isUsed) {
                                                message('该区域下还有其他房台，请先删除', $this->createWebUrl('table'), 'success');
                                            }
                                            pdo_delete('j_hmoney_tablecategory', array('id' => $id));
                                        }
                                        message('删除成功', $this->createWebUrl('table'), 'success');
                                    } else {
                                        if ($operation == 'deletetable') {
                                            $id = intval($_GPC['id']);
                                            if ($id) {
                                                $isUsed = pdo_fetchcolumn('SELECT status FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $id));
                                                if ($isUsed) {
                                                    message('该房台还在使用中', $this->createWebUrl('table'), 'success');
                                                }
                                                pdo_delete('j_hmoney_table', array('id' => $id));
                                            }
                                            message('删除成功', $this->createWebUrl('table'), 'success');
                                        } else {
                                            if ($operation == 'addnumtable') {
                                                $title = $_GPC['title'];
                                                $num = intval($_GPC['num']);
                                                if (!$num || $num > 99) {
                                                    exit(json_encode(array('success' => false, 'msg' => '请输入要添加的数量')));
                                                }
                                                $maxnum = intval($_GPC['maxnum']);
                                                $pcate = intval($_GPC['pcate']);
                                                $perperson = intval($_GPC['perperson']);
                                                $titleAry = array();
                                                $i = 1;
                                                while ($i <= $num) {
                                                    $titleAry[] = $title . $i;
                                                    $i = $i + 1;
                                                }
                                                $isUsed = pdo_fetchall('SELECT title FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and title in(\'' . implode('\',\'', $titleAry) . '\')');
                                                foreach ($isUsed as $row) {
                                                    $key = array_search($row['title'], $titleAry);
                                                    if ($key !== false) {
                                                        array_splice($titleAry, $key, 1);
                                                    }
                                                }
                                                if (count($titleAry) == 0) {
                                                    exit(json_encode(array('success' => false, 'msg' => '所有添加的房台均已存在')));
                                                }
                                                $sql = 'insert into ' . tablename('j_hmoney_table') . ' (weid,groupid,title,maxnum,pcate,perperson)VALUES';
                                                $ary = array();
                                                $num = 0;
                                                foreach ($titleAry as $row) {
                                                    $temp = array(0 => $_W['uniacid'], 1 => $shopid, 2 => $row, 3 => $maxnum, 4 => $pcate, 5 => $perperson);
                                                    $ary[] = '(\'' . implode('\',\'', $temp) . '\')';
                                                }
                                                $sql = $sql . implode(',', $ary);
                                                pdo_run($sql);
                                                exit(json_encode(array('success' => true, 'num' => count($titleAry))));
                                            } else {
                                                if ($operation == 'getqrcode') {
                                                    $item = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and qrlink=\'\'');
                                                    exit(json_encode(array('success' => true, 'num' => $item)));
                                                } else {
                                                    if ($operation == 'createqrcode') {
                                                        if (!$shopid) {
                                                            exit(json_encode(array('success' => false, 'msg' => '请先选择店铺')));
                                                        }
                                                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = ' . $_W['uniacid'] . ' and groupid =\'' . $shopid . '\' and qrlink=\'\' order by id desc limit 1');
                                                        if (!$item) {
                                                            exit(json_encode(array('success' => true, 'ismore' => false)));
                                                        }
                                                        $link = $_W['siteroot'] . 'app/index.php?i=' . $_W['uniacid'] . '&c=entry&do=placeorder&m=j_hmoney&shopid=' . $shopid . '&tid=' . $item['id'];
                                                        $url = trueUrl2Shorturl($link);
                                                        if ($url) {
                                                            pdo_update('j_hmoney_table', array('qrlink' => $url), array('id' => $item['id']));
                                                        }
                                                        exit(json_encode(array('success' => true, 'ismore' => true, 'url' => $url)));
                                                    } else {
                                                        if ($operation == 'output') {
                                                            $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by displayorder asc,id desc');
                                                            $categoryary = array();
                                                            foreach ($category as $row) {
                                                                $categoryary[$row['id']] = $row['title'];
                                                            }
                                                            $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by id desc ');
                                                            require_once '../framework/library/phpexcel/PHPExcel.php';
                                                            $objPHPExcel = new PHPExcel();
                                                            $objPHPExcel->getProperties()->setCreator('智慧收银台')->setLastModifiedBy('智慧收银台')->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('备份数据')->setKeywords('excel')->setCategory('result file');
                                                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '所在区域')->setCellValue('B1', '名称')->setCellValue('C1', '二维码');
                                                            $num = 2;
                                                            foreach ($list as $row) {
                                                                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $num, $categoryary[$row['pcate']])->setCellValue('B' . $num, $row['title'])->setCellValue('C' . $num, $row['qrlink']);
                                                                $num = $num + 1;
                                                            }
                                                            $objPHPExcel->getActiveSheet()->setTitle('餐桌列表');
                                                            $objPHPExcel->setActiveSheetIndex(0);
                                                            ob_end_clean();
                                                            header('Content-Type: application/vnd.ms-excel');
                                                            header('Content-Disposition:attachment;filename="table_temp.xls"');
                                                            header('Cache-Control:max-age=0');
                                                            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                                                            $objWriter->save('php://output');
                                                            exit(0);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/table');